package com.ydw;

import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Yangdw
 * @date 2025-08-31
 * @description:
 */
public class PartlyProducer {

    public static void main(String[] args) throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer("test-producer-group",true);
        producer.setNamesrvAddr("127.0.0.1:9876");
        JSONObject data = new JSONObject();
        data.put("message","你好好");
        producer.start();
        try {
            {

                List<Order> orderList1 = buildList("1", "Buy", "Pay", "Finish");

                List<Order> orderList2 = buildList("2", "Buy", "Pay");
                List<Order> orderList3 = buildList("3",  "Pay", "Finish");


                List<Order> allList = new ArrayList<>(9);
                allList.addAll(orderList1);
                allList.addAll(orderList2);
                allList.addAll(orderList3);

                for (int i = 0; i < allList.size(); i++) {
                    Message msg = new Message("Partly-Oraderly-Topic",
                            "TagA",
                            JSONObject.toJSONString(allList.get(i)).getBytes(RemotingHelper.DEFAULT_CHARSET));
                    msg.setKeys("partly_global");

                    SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
                        @Override
                        public MessageQueue select(List<MessageQueue> list, Message message, Object o) {

                            int orderId = Integer.parseInt(o.toString());
                            int idx = orderId % list.size();

                            return list.get(idx);
                        }
                    }, allList.get(i).getOrderId());
                    System.out.printf("%s%n", sendResult);
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static List<Order> buildList(String orderId,String ...descList){
        List<Order> orderList = new ArrayList<>(3);
        for (String s : descList) {
            Order order = new Order(orderId,s);
            orderList.add(order);
        }
        return orderList;
    }

    static class Order{
        private String orderId;
        private String desc;

        public Order(String orderId, String desc) {
            this.orderId = orderId;
            this.desc = desc;
        }

        public String getOrderId() {
            return orderId;
        }

        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
}
